<template>
	<div>
		<ul class="aui-list aui-form-list aui-margin-b-15">
			<!--<li class="aui-list-item" v-if="false">
            	<div class="aui-list-item-inner" @click="goTomap">
                    <div class="aui-list-item-label">选择地区</div>
                    <div class="aui-list-item-input aui-list-item-arrow">
                        <div id="name" class="addressText" style="width: 100%;color: #757575;"> {{addressDetail?addressDetail:'如：北京市 市辖区 昭阳区'}} </div>
                    </div>
                </div>
            </li>-->
          
			<li class="aui-list-item menpai" @click="goTomap">
				<div class="aui-list-item-inner">
					<!--<div class="aui-list-item-label">详细地址</div>-->
					<div class="aui-list-item-label">收货地址</div>
					<div class="aui-list-item-input" style="text-align: left;">
						<p placeholder="如：圣城街道怡景小区5栋402" id="detail-address" >{{address?address:'选择地址信息'}}</p>
					</div>
				</div>
			</li>
			<li class="aui-list-item">
            	<div class="aui-list-item-inner">
                    <div class="aui-list-item-label">门牌号</div>
                    <div class="aui-list-item-input">				<!--houseNumber-->
						<input type="text" placeholder="门牌号码（选填）" v-model="houseNumber" id="addressHouseNumber" maxlength="10">
					</div>
                </div>
            </li>
		</ul>
		
		<ul class="aui-list aui-form-list aui-margin-b-15">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">姓名 </div>
					<div class="aui-list-item-input">				<!--receivername-->
						<input type="text" placeholder="输入收货人姓名" v-model="receivername" id="addressUserName" maxlength="10">
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner" ref="iosTouch" @click="iosTouchClickEvent" @touchstart="iosTouchTouchstartEvent">
					<div class="aui-list-item-label">性别</div>
					<div class="aui-list-item-input addressText" id="Sex">
						<label><input class="aui-radio" type="radio" name="demo1" :checked="sex=='男' || sex== 0" @click.stop="getSex(0)"><span class="labelMargin">先生</span></label> &nbsp;&nbsp;
						<label><input class="aui-radio" type="radio" name="demo1" :checked="sex=='女' || sex== 1" @click.stop="getSex(1)"><span class="labelMargin">女士</span></label>
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">手机号码</div>
					<div class="aui-list-item-input">
						<input type="number" placeholder="输入收货人手机号码" v-model="phone" id="addressMobile">
					</div>
				</div>
			</li>
		</ul>
		<ul class="aui-list aui-form-list aui-margin-b-15">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">标签</div>
					<div style="width: 100%" class="address-tab-box addressText">
						<label><input class="aui-radio" type="radio" name="address-tab" :checked="label=='家庭' || label==0" @click.stop="getLabel(0)"><span class="labelMargin">家庭</span></label>&nbsp;&nbsp;
						<label><input class="aui-radio" type="radio" name="address-tab" :checked="label=='公司' || label==1" @click.stop="getLabel(1)"><span class="labelMargin">公司</span></label>&nbsp;&nbsp;
						<label><input class="aui-radio" type="radio" name="address-tab" :checked="label=='其他' || label==2" @click.stop="getLabel(2)"><span class="labelMargin">其他</span></label>
					</div>
				</div>
			</li>
			<!--<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<label><input class="aui-checkbox" type="checkbox" name="demo1" ref='checkedBo' id="defaultAddress" :checked="defaultaddress=='默认'" @click.stop="setDefault">&nbsp;设为默认地址</label>
				</div>
			</li>-->
		</ul>
	</div>
</template>

<script>
	
	import {mapActions,mapMutations,mapGetters} from 'vuex';
	import axios from 'axios';
	export default {
		props:{
//			addFlag: {
//				type: Boolean,
//              default:true
//			},
//			getNowAddressPage: {
//				type: Object,
//				default: {}
//			}
//			item: {
//              type: Object,
//              default:()=>{
//              	return {}
//              }
//          },
//          userInfomation: {
//          	type: Object,
//              default: ()=>{
//              	return {}
//              }
//          },
//          getstatusBtn: {
//          	type: Function,
//              default: () => {}
//          }
		},
		computed:{
			...mapGetters('zx',[
				"getaddFlag","getNowAddress"
			]),
		},
		data() {
			return {
				address:'', //详细地址 。。街道信息
				addressDetail:null, // 省市区信息
				province:null, //省
				city:null, // 城市
				region:null, //区域
				mapFlag:false,
				positionlat:null, //纬度
				positionlng:null, // 经度
				result:null, // 地址详细信息
				houseNumber:null, // 门牌号
				receivername:null, // 收货人姓名
				sex:null,//收货人性别
				phone:'', //联系电话
				label:null,// 标签
				addFlag:true,//是否为新增地址  true新增 false 编辑	
				obj:null,
				getNowAddressPage:null,
			}
		},
		watch:{
			receivername(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.receivername = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('provinceId',nvalue,ovalue);
			},
			address(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.address = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('provinceId',nvalue,ovalue);
			},
			addressDetail(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.addressDetail = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('provinceId',nvalue,ovalue);
			},
			sex(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.sex = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('provinceId',nvalue,ovalue);
			},
			phone(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.phone = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('provinceId',nvalue,ovalue);
			},
			label(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.label = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
				console.log('label',nvalue,ovalue);
			},
			province(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.province = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('provinceId',nvalue,ovalue);
			},
			city(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.city = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('cityId',nvalue,ovalue);
			},
			region(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.region = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('region',nvalue,ovalue);
			},
			address(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.address = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('addressDetail',nvalue,ovalue);
			},
			positionlng(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.positionlng = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('positionlng',nvalue,ovalue);
			},
			positionlat(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.positionlat = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
//				console.log('positionlat',nvalue,ovalue);
			},
			result(nvalue,ovalue){
//				console.log("添加详情.....",nvalue,ovalue)
				let nowAddressSinger = {};
				nowAddressSinger.result = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
			},
			houseNumber(nvalue,ovalue){
				let nowAddressSinger = {};
				nowAddressSinger.houseNumber = nvalue ;
				this.setNowAddressMutationSinger(nowAddressSinger);
			}
		},
		methods:{
			...mapMutations('zx',[
				"setNowAddressMutationSinger",'setNowAddressMutation'
			]),
			...mapMutations('wxh', [
			  'USER_INFO',
			  'LOGO_OUT',
			  'LOGO_IN',
			  'CLEAR_INFO'
			]),
			iosTouchClickEvent(){
				console.log('iosTouchClickEvent');
			},
			iosTouchTouchstartEvent(){
				console.log('iosTouchTouchstartEvent');
			},
			getAddressInfo(){
//				let obj = this.$route.query;
//				if(obj){
//					console.log(obj.type,'555');
//				}
//				if(this.getNowAddressPage.result && !this.addFlag){
//					let arr = this.getNowAddressPage.result.split(" ");
//					this.addressDetail = arr[0]+' '+ arr[1]+ ' '+ arr[2];
//					this.address = arr[3]
//					console.log(arr,'arrarrarr');
//					
//				}
				
			},
//			getType(list) {
//	            let arr = []
//	            list && list.map((item) => {
//	                arr.push({text: item.name,value: item.id})
//	            })
//	            return arr
//	        },
			async getArea (code) {
//				var url = 'http://172.19.6.152:33001/address/select/'
//	            let res = await axios.get(window.api.mall_base_host + '/address/getarealist?areaid=' + code)
	            let res = await axios.get(window.api.base_host+'/address/select/' + code)
	            return res.data.data
	        },
	        goTomap(){
	        	if(this.addFlag){
	        		if(this.$route.query.toFrom){
	        			this.$router.push({path:'map',query:{type:'add',toFrom:'chooseAddress'}});
	        		}else if(this.$route.query.id != undefined){
	        			if(this.$route.query.id !=2.2){
	        				this.$router.push({path:'map',query:{type:'add',id:1}});
	        			}else if(this.$route.query.id ==2.2){
	        				this.$router.push({path:'map',query:{type:'add',id:1.2}});
	        			}
	        		}else{
	        			this.$router.push({path:'map',query:{type:'add'}});
	        		}
	        	}else{
	        		console.log(this.getNowAddressPage,'当前地址去');
	        		if(this.getNowAddressPage.positionlng || this.getNowAddressPage.positionlat){
	        			var positionlng = this.getNowAddressPage.positionlng;
	        			var positionlat = this.getNowAddressPage.positionlat;
	        		}
	        		if(this.$route.query.id != undefined){
	        			if(this.$route.query.id != 2.2){
	        				this.$router.push({path:'map',query:{id:1,type:'edit',positionlng:positionlng,positionlat:positionlat,addressDetail:this.addressDetail}});
	        			}else if(this.$route.query.id == 2.2){
	        				this.$router.push({path:'map',query:{id:1.2,type:'edit',positionlng:positionlng,positionlat:positionlat,addressDetail:this.addressDetail}});
	        			}
	        		}else{
	        			this.$router.push({path:'map',query:{type:'edit',positionlng:positionlng,positionlat:positionlat,addressDetail:this.addressDetail}});
	        		}
	        	}
	        },
	        //初始化经纬度
	        innitPingLan(){
//	        	var obj = this.getNowAddressPage;
//	        	obj.positionlng = this.positionlng;
//	        	obj.positionlat = this.positionlat;
//	        	this.houseNumber = obj.houseNumber;
//	        	this.setNowAddressMutation(obj);
	        },
	        getQuery(){
//	        	console.log('进来看看');
				this.getNowAddressPage = this.getNowAddress;
				this.addFlag = this.getaddFlag;
				this.setNowAddressType(); //为了实现保留用户新增是填写的数据，与编辑所存做区分
	        	if(this.$route.query.obj != undefined){
	        		this.mapFlag = true;
	        		console.log(this.$route.query.obj,'快速編輯地址');
//	        		console.log(this.getNowAddressPage,'当前地址回');
	        		this.obj = JSON.parse(this.$route.query.obj);
	        		this.address = this.obj.address;
	        		this.addressDetail = this.obj.province + ' ' + this.obj.city + ' ' + this.obj.region;
	        		this.result = this.obj.province + ' ' + this.obj.city + ' ' + this.obj.region + " " + this.obj.address;
//	        		console.log(this.result,'详细地址++++')
	        		this.houseNumber = this.getNowAddressPage.houseNumber; // 门牌号
	        		this.receivername = this.getNowAddressPage.receivername; // 收货人姓名
	        		this.phone = this.getNowAddressPage.phone; //联系电话
	        		this.positionlng = this.obj.positionlng;
	        		this.positionlat = this.obj.positionlat;
					this.getLabel(this.getNowAddressPage.label);// 标签
					this.getSex(this.getNowAddressPage.sex); //收货人性别
//	        		console.log(this.address,'編輯好事心中');
					this.addAddressQuicker(this.obj);


	        	}else{
	        		this.positionlng = this.getNowAddressPage.positionlng;
	        		this.positionlat = this.getNowAddressPage.positionlat;
	        		this.address = this.getNowAddressPage.address;
					this.addressDetail= this.getNowAddressPage.result;
					this.province = this.getNowAddressPage.province;
					this.city = this.getNowAddressPage.city;
					this.region = this.getNowAddressPage.region;
					this.result = this.getNowAddressPage.result;
					this.houseNumber = this.getNowAddressPage.houseNumber; // 门牌号
					this.receivername = this.getNowAddressPage.receivername; // 收货人姓名
					this.phone = this.getNowAddressPage.phone; //联系电话
					console.log('this.getNowAddressPage.label',this.getNowAddressPage.label)
					this.getLabel(this.getNowAddressPage.label);// 标签
					this.getSex(this.getNowAddressPage.sex); //收货人性别
					
	        	}
	        },
	        getLabel(num){
				if(num == 0 || num ==  '家庭'){
					this.label = '家庭';
				}else if(num == 1 || num == '公司'){
					this.label = '公司';
				}else if(num == 2 || num == '其他'){
					this.label = '其他';
				}
			},
			getSex(type){
				if(type == 0 || type == '男'){
					this.sex = '男';
				}else if(type == 1 || type == '女'){
					this.sex = '女';
				}

			},
			setDefault(){
				if(this.$refs.checkedBo.checked){
					this.defaultaddress = '默认';
				}else{
					this.defaultaddress = '非默认';
				}
			},
			//设置当前地址的类型
			setNowAddressType(){
				let nowAddressSinger = {};
				if(this.addFlag){
					nowAddressSinger.type =  'add';
				}else{
					nowAddressSinger.type =  'edit';
				}
				this.setNowAddressMutationSinger(nowAddressSinger);
			},
			//获取区域编码
			async getAreaId(code){
				var arr = [];
				var url = window.api.base_host+'/address/select/';
				await axios.get(url + code).then((res)=>{
					arr = res.data.data;
				})
				return arr;
			},
//			//匹配出当前的区域编码
//			checkedArea(arr,name){
//				var code = null;
//				for(let i = 0; i < arr.length; i++){
//					if(arr[i].name === name){
//						code = arr[i].rid;
//					}
//				}
//				return code;
//			},
//			async addAddressQuicker2(obj){
//				 var provinceArr = await this.getAreaId(1);
//				  console.log(provinceArr,'城市名')
//				 var provinceName = obj.province;
//				 this.province = this.checkedArea(provinceArr,provinceName);
//				
//				 var cityArr = await this.getAreaId(this.province);
//				 var cityName = obj.city;
//				 this.city = this.checkedArea(cityArr,cityName);
//				 
//				 var regionArr = await this.getAreaId(this.city);
//				 var regionName = obj.region;
//				 this.region = this.checkedArea(regionArr,regionName);
//			},
			//直辖市特别处理
			checkedCityCode(code1,code2,name){
				var promiseA = new Promise(function(resolver,reject){
					
				});
				var that = this;
				var p = Promise.all([this.getAreaId(code1),this.getAreaId(code2)]);
				
				p.then(function(val) {
//				    console.log(val,'8888888');
					var arr = val[0].concat(val[1]);
					var arr1 = val[0];
					var arr2 = val[1];
					for(let i = 0; i < arr.length;i++){
						if(name === arr[i].name){
							that.region = arr[i].rid;
						}
					}
					for(let j = 0; j < arr1.length;j++){
						if(that.region == arr1[j].rid){
							that.city = code1;
						}
					}
					for(let k = 0; k < arr2.length;k++){
						if(that.region == arr2[k].rid){
							that.city = code2;
						}
					}
//					console.log(arr,that.city,that.region,'城市编码')
				});
				
				
			},
	        addAddressQuicker(obj){
	        	if(obj){
	        		var provinceId = null;
//	        		let url = window.api.mall_base_host + '/address/getarealist?areaid=' + 1   erp接口
					var url = window.api.base_host+'/address/select/'
//	        		http://172.19.5.89:33001/address/select/1 
			        axios.get(url + 1).then((res)=>{
			      
			        	var province = res.data.data;
			        	for(let i = 0; i < province.length; i++){
			        		console.log(obj.province,'obj.province')
			        		if(obj.province == province[i].name){
//								console.log(province[i].name,'省')
			        			this.province = province[i].rid;
			        			axios.get(url + province[i].rid).then((res)=>{
			        				var city = res.data.data;
			        				
			        				//由于高德地图与后台的编码方式不一致  如果属于直辖市就对后台地址进行处理再匹配 北京 天津 上海 重庆
			        				if(this.province === 2 || this.province === 3 || this.province === 10 || this.province === 23){
			        					this.checkedCityCode(city[0].rid,city[1].rid,obj.region);
			        				}else{
			        					for(let j = 0; j < city.length; j++){
				        					if(obj.city === city[j].name){
	//			        						console.log(city[j].name,'市')
	//			        						console.log(city[j].rid,'city[j].name');
				        						this.city = city[j].rid;
				        						axios.get(url + city[j].rid).then((res)=>{
				        							var region = res.data.data;
				        							for(let k = 0 ;k < region.length; k++){
				        								if(obj.region === region[k].name){
	//			        									console.log(region[k].name,'区')
				        									this.region = region[k].rid;
				        								}
				        							}
				        						})
				        					}
				        				}
			        				}
			        				
			        			})
			        		}
			        	}
			        })
	        	}
	        },
		},
		created(){
			this.getQuery();
			
		},
		mounted(){
			this.getAddressInfo();
			this.innitPingLan();
		},
		beforeCreate(){
	       
	    },
	    beforeDestroy(){
	       
	    },
	}
</script>

<style scoped="scoped">
	.addressText{
		text-align: left;
	}
	
</style>